package com.weayer.service;

import com.weayer.dto.DriverUserWorkStatus;
import com.weayer.dto.ResponseResult;
import com.weayer.mapper.DriverUserMapper;
import com.weayer.mapper.DriverUserWorkStatusMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: Service服务 - 司机 - 工作状态管理Service
 * @author: Weayer
 **/
@Service
public class DriverUserWorkStatusService{
    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;

    public ResponseResult addWorkStatus(Long driverId, Integer workStatus){
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("driver_id", driverId);
        List<DriverUserWorkStatus> driverUserWorkStatuses = driverUserWorkStatusMapper.selectByMap(queryMap);
        if(driverUserWorkStatuses.size() != 0){
            return changeWorkStatus(driverId, workStatus);
        }else{
            DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
            driverUserWorkStatus.setDriverId(driverId);
            driverUserWorkStatus.setWorkStatus(workStatus);
            LocalDateTime now = LocalDateTime.now();
            driverUserWorkStatus.setGmtCreate(now);
            driverUserWorkStatus.setGmtModified(now);
            driverUserWorkStatusMapper.insert(driverUserWorkStatus);
            return ResponseResult.success("");
        }
    }
    public ResponseResult changeWorkStatus(Long driverId, Integer workStatus){
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("driver_id", driverId);
        List<DriverUserWorkStatus> driverUserWorkStatuses = driverUserWorkStatusMapper.selectByMap(queryMap);
        DriverUserWorkStatus driverUserWorkStatus = driverUserWorkStatuses.get(0);
        driverUserWorkStatus.setWorkStatus(workStatus);
        LocalDateTime now = LocalDateTime.now();
        driverUserWorkStatus.setGmtModified(now);
        driverUserWorkStatusMapper.updateById(driverUserWorkStatus);
        return ResponseResult.success("");
    }

}
